package com.example.mail.orderservice.rest;


import com.example.account.dto.AccountDTO;
import com.example.account.feign.AccountFeign;
import com.example.product.dto.ProductDTO;
import com.example.product.feign.ProductFeign;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mail.orderservice.service.OrderService;
import com.example.rest.RestBody;
import com.example.rest.RestCode;
import com.example.mail.orderservice.entity.Order;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhou
 * @since 2022-01-10
 */
@AllArgsConstructor
@RestController
@RequestMapping("/order")
public class OrderController {


    private final OrderService orderService;
    private final AccountFeign accountFeign;
    private final ProductFeign productFeign;

    /**
     * 分页查询 Order 对象
     * @return Rest
     */
    @GetMapping("/{code}")
    public RestBody<?> list(@PathVariable("code")String code){
        Order one = this.orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo,code)
        );
        

        return RestBody.ok(one);
    }

    /**
     * 新增或修改 Order 实体接口
     * @return Rest
     */
    @PostMapping
    public RestBody<?> createOrUpdate(@Valid @RequestBody Order order){

        this.orderService.save(order);

        this.orderService.createOrder(order);

        return RestBody.ok();
    }

    /**
     * 逻辑删除一条 实体，
     * 注意当且仅当调用 mybatis plus 内置方法时可以自动处理
     * @param id 资源对应id主键
     * @return Rest
     */
    @DeleteMapping("/{id}")
    public RestBody<?> delete(@PathVariable("id")  Long id){
        //TODO 添加自己的删除校验逻辑
        boolean removed = this.orderService.removeById(id);
        //Restful风格建议，无可操作数据时，返回404资源未找到提示
        return removed ? RestBody.ok() : RestBody.fail(RestCode.RESOURCE_NOT_FOUND);
    }

}

